package cn.netdisk.tinynetdisk.common.entity;

import cn.netdisk.tinynetdisk.common.enums.CodeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 统一的接口回复类
 *
 * @Author: chenlb
 */
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
//保证序列化json时,如果是null的对象,key也会消失
public class ResponseData<T> implements Serializable {

    /**
     * 返回码
     */
    private int code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 返回总条数
     */
    private Long count;

    /**
     * 返回数据
     */
    private T data;

    private ResponseData(int code) {
        this.code = code;
    }

    private ResponseData(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseData(int code, long count, T data) {
        this.code = code;
        this.count = count;
        this.data = data;
    }

    private ResponseData(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ResponseData(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess(){
        return  this.code == CodeEnum.SUCCESS.getCode();
    }

    public int getcode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ResponseData<T> createBySuccess(){
        return new ResponseData<T>(CodeEnum.SUCCESS.getCode());
    }

    public static <T> ResponseData<T> createBySuccessMessage(String msg){
        return new ResponseData<T>(CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ResponseData<T> createBySuccess(T data){
        return new ResponseData<T>(CodeEnum.SUCCESS.getCode(), data);
    }

    public static <T> ResponseData<T> createBySuccessToPagination(Long count, T data){
        return new ResponseData<T>(CodeEnum.SUCCESS.getCode(), count, data);
    }

    public static <T> ResponseData<T> createBySuccess(String msg, T data){
        return new ResponseData<T>(CodeEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResponseData<T> createByError() {
        return new ResponseData<T>(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getDesc());
    }

    public static <T> ResponseData<T> createByErrorMessage(String errorMessage) {
        return new ResponseData<T>(CodeEnum.ERROR.getCode(), errorMessage);
    }

    public static <T> ResponseData<T> createByErrorEnum(CodeEnum codeEnum) {
        return new ResponseData<T>(codeEnum.getCode(), codeEnum.getDesc());
    }

    public static <T> ResponseData<T> createByErrorMessage(int errorCode, String errorMessage) {
        return new ResponseData<T>(errorCode, errorMessage);
    }

    public static <T> ResponseData<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ResponseData<T>(errorCode, errorMessage);
    }
}